package hu.debrecen.adastra.card.web;

import hu.debrecen.adastra.card.repository.domain.Account;
import hu.debrecen.adastra.card.service.SocialUserService;
import hu.debrecen.adastra.card.service.achievement.AchievementService;
import hu.debrecen.adastra.card.web.domain.AchievementVO;
import hu.debrecen.adastra.card.web.resolver.AutowiredHandlerAnnotationArgumentResolver.AutowiredHandler;

import java.util.List;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

@Controller
public class AchievementController extends AbstractController {

	private static final Logger LOGGER = LoggerFactory.getLogger(AchievementController.class);

	private final AchievementService achievementService;

	@Inject
	public AchievementController(final SocialUserService socialUserService, final AchievementService achievementService) {
		super(socialUserService);
		this.achievementService = achievementService;
	}

	@RequestMapping(value = { "/achievements" }, method = RequestMethod.GET)
	@ResponseBody
	public List<AchievementVO> getAllAchievements(@AutowiredHandler final Account account) {
		AchievementController.LOGGER.trace("get all achievements");

		final List<String> achievedAchievements = this.achievementService.getAchievements(account);
		return Lists.newLinkedList(Iterables.transform(this.achievementService.getAchievements(), new Function<String, AchievementVO>() {

			@Override
			public AchievementVO apply(final String achievement) {
				return new AchievementVO(achievement, achievedAchievements.contains(achievement));
			}

		}));
	}

	@RequestMapping(value = { "/achievements" }, params = "type=achieved", method = RequestMethod.GET)
	@ResponseBody
	public List<String> getAchivedAchievementsByPlayers(@AutowiredHandler final Account account) {
		AchievementController.LOGGER.trace("get achieved achievements by account({}) and type({})", account, "achieved");
		return this.achievementService.getAchievements(account);
	}

	@RequestMapping(value = { "/achievements" }, params = "type=new", method = RequestMethod.GET)
	@ResponseBody
	public List<String> getNewAchievementsByPlayers(@AutowiredHandler final Account account) {
		AchievementController.LOGGER.trace("get new achievements by account({}) and type({})", account, "new");
		return this.achievementService.getNewAchievements(account);
	}
}
